import { addClientGradeApi, editClientGradeApi } from "@/api/configCenter/clientGrade";
import FormHook from "@/hook/formHook";
import { GeneraDataType } from "@/interface";
import { message } from "ant-design-vue";
export default function OperationHook(state: GeneraDataType) {
  const { formState, formDataRef, emit, type, passData, loading } = state;
  const { resetFields } = FormHook({ formState });
  const onFinish = () => {
    let requestFn;
    const params = {
      ...formState
    };
    if (type.value === 1) {
      requestFn = addClientGradeApi;
      Reflect.deleteProperty(params, "id");
    } else {
      requestFn = editClientGradeApi;
      params.id = passData.value.id;
    }
    loading.value = true;
    requestFn(params)
      .then((res: any) => {
        const { code } = res;
        if (code === 200) {
          message.success("操作成功!");
          emit("submit");
        }
      })
      .finally(() => (loading.value = false));
  };

  const onFinishFailed = (errorInfo: any) => {
    console.log("Failed:", errorInfo);
  };
  const resetHandle = () => {
    resetFields();
    formDataRef.value.clearValidate();
    emit("update:visible", false);
  };
  return {
    onFinish,
    onFinishFailed,
    resetHandle,
    resetFields
  };
}
